package com.meida.module.bus.provider.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.util.TypeUtils;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusReportOutone;
import com.meida.module.bus.provider.mapper.BusReportOutoneMapper;
import com.meida.module.bus.provider.service.BusReportOutoneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 接口实现类
 *
 * @author flyme
 * @date 2020-11-21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusReportOutoneServiceImpl extends BaseServiceImpl<BusReportOutoneMapper, BusReportOutone> implements BusReportOutoneService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReportOutone bro, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReportOutone> cq, BusReportOutone bro, EntityMap requestMap) {
        cq.eq("bro.projectCode", requestMap.get("projectCode"));
        cq.like("bro.reportMonth", requestMap.get("reportMonth"));
        cq.like("bro.batchNumber", requestMap.get("batchNumber"));
        cq.orderByDesc("bro.reportMonth,bro.batchNumber");
        cq.createJoin(BusProject.class).setMainField("projectCode").setJoinField("projectCode");
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> getExportData(Map params) {
        CriteriaQuery<BusReportOutone> cq = new CriteriaQuery(BusReportOutone.class);
        cq.eq("projectCode", params.get("projectCode"));
        cq.eq("reportMonth", params.get("reportMonth"));
        cq.eq("batchNumber", params.get("batchNumber"));
        return selectEntityMap(cq);
    }

    @Override
    public ResultBody importBusReportOutOneData(MultipartFile file, Map params) {
        try {
            String reportMonth = (String) params.get("reportMonth");
            Integer batchNumber = TypeUtils.castToInt(params.get("batchNumber"));
            String projectCode = (String) params.get("projectCode");
            ImportParams importParams = new ImportParams();
            List<Map<Object, Object>> ExcelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, importParams);
            List<BusReportOutone> busReportOutoneList = new ArrayList<>();
            for (Map<Object, Object> objectMap : ExcelList) {
                String otherField1 = TypeUtils.castToString(objectMap.get("备用"));
                String projectNo = TypeUtils.castToString(objectMap.get("条目编号"));
                String projectDescEn = TypeUtils.castToString(objectMap.get("项目描述[设备/构成/明细]-英文"));
                String projectDescCn = TypeUtils.castToString(objectMap.get("项目描述[设备/构成/明细]-中文"));
                String buildingCodes = TypeUtils.castToString(objectMap.get("建筑编号"));
                String professional = TypeUtils.castToString(objectMap.get("专业"));
                String type = TypeUtils.castToString(objectMap.get("类型"));
                String divisionOfEngineering = TypeUtils.castToString(objectMap.get("分部工程"));
                String sectionalWorks = TypeUtils.castToString(objectMap.get("分项工程"));
                String brand = TypeUtils.castToString(objectMap.get("品牌"));
                String supplier = TypeUtils.castToString(objectMap.get("供应商/厂家"));
                String remark = TypeUtils.castToString(objectMap.get("备注"));
                String size = TypeUtils.castToString(objectMap.get("尺寸"));
                String equipmentSerialNumber = TypeUtils.castToString(objectMap.get("设备编号"));
                String material = TypeUtils.castToString(objectMap.get("材质"));
                String capacity = TypeUtils.castToString(objectMap.get("容量/能力"));
                String capacityUnit = TypeUtils.castToString(objectMap.get("容量单位/能力单位"));
                String unit = TypeUtils.castToString(objectMap.get("单位"));
                BigDecimal contractNum = TypeUtils.castToBigDecimal(objectMap.get("合约数量"));
                BigDecimal totalFinshNum = TypeUtils.castToBigDecimal(objectMap.get("前期累计完成数量"));
                BigDecimal remainNoFinshNum = TypeUtils.castToBigDecimal(objectMap.get("剩余未完成数量"));
                String state = TypeUtils.castToString(objectMap.get("状态"));
                BigDecimal totalFinshNumByMw = TypeUtils.castToBigDecimal(objectMap.get("累计完成数量（MW商务填写）"));
                BigDecimal totalFinshNumBySupplier = TypeUtils.castToBigDecimal(objectMap.get("累计完成数量（供应商填写）"));
                String itemType = TypeUtils.castToString(objectMap.get("条目类型"));
                String checkState = TypeUtils.castToString(objectMap.get("审核状态"));
                BusReportOutone busReportOutone = new BusReportOutone();
                busReportOutone.setOtherField1(otherField1);
                busReportOutone.setReportMonth(reportMonth);
                busReportOutone.setBatchNumber(batchNumber);
                busReportOutone.setProjectCode(projectCode);
                busReportOutone.setProjectNo(projectNo);
                busReportOutone.setProjectDescEn(projectDescEn);
                busReportOutone.setProjectDescCn(projectDescCn);
                busReportOutone.setBuildingCodes(buildingCodes);
                busReportOutone.setProfessional(professional);
                busReportOutone.setType(type);
                busReportOutone.setDivisionOfEngineering(divisionOfEngineering);
                busReportOutone.setSectionalWorks(sectionalWorks);
                busReportOutone.setBrand(brand);
                busReportOutone.setSupplier(supplier);
                busReportOutone.setRemark(remark);
                busReportOutone.setSize(size);
                busReportOutone.setEquipmentSerialNumber(equipmentSerialNumber);
                busReportOutone.setMaterial(material);
                busReportOutone.setCapacity(capacity);
                busReportOutone.setCapacityUnit(capacityUnit);
                busReportOutone.setUnit(unit);
                busReportOutone.setContractNum(contractNum);
                busReportOutone.setTotalFinshNum(totalFinshNum);
                busReportOutone.setRemainNoFinshNum(remainNoFinshNum);
                busReportOutone.setState(state);
                busReportOutone.setTotalFinshNumByMw(totalFinshNumByMw);
                busReportOutone.setTotalFinshNumBySupplier(totalFinshNumBySupplier);
                busReportOutone.setItemType(itemType);
                busReportOutone.setCheckState(checkState);
                busReportOutoneList.add(busReportOutone);
            }
            if (busReportOutoneList.size() > 0) {
                boolean b = saveBatch(busReportOutoneList);
                if (b) {
                    return ResultBody.ok("导入成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("导入失败");
    }
}
